package com.cdshop.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import com.cdshop.comm.BusinessException;
import com.cdshop.entity.User;
import com.cdshop.web.auth.AuthenticateService;
import com.cdshop.web.auth.Identity;

public class LoginPM extends AbstractPM {

	private static final long serialVersionUID = 1L;

	private String username;
	private String password;
	private boolean loggedIn;

	@Override
	public void populate(PageContext pctx) throws BusinessException {
		HttpServletRequest request = (HttpServletRequest) pctx.getRequest();
		AuthenticateService auth = AuthenticateService.instance();
		HttpSession session = request.getSession();
		loggedIn = auth.isLoggedIn(session);
		if (loggedIn) {
			Identity identity = auth.getIdentity(session);
			User user = identity.getUser();
			username = user.getUsername();
			password = null;
		} else {
			username = null;
			password = null;
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

}
